<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Activity extends Model
{
    //
    protected $table = 'activity';

    protected $guarded = ['process'];


    public function getStatusAttribute($value)
    {
        $statusMap = [
            0 => '准备中',
            1 => '报名中',
            2 => '已结束',
        ];

        return $statusMap[$value] ?? '未知状态';
    }

    public function getCheckRuleAttribute($value)
    {
        $statusMap = [
            0 => '无需审核',
            1 => '需要审核',
        ];

        return $statusMap[$value] ?? '未知状态';
    }

    public function getCancelRuleAttribute($value)
    {
        $statusMap = [
            0 => '可取消',
            1 => '不可取消',
        ];

        return $statusMap[$value] ?? '未知状态';
    }


    public function getKeysAttribute($value)
    {
        return json_decode($value, true); // 将JSON字符串转换为PHP数组
    }

    public function setKeysAttribute($value)
    {
        $this->attributes['keys'] = json_encode($value); // 将PHP数组转换为JSON字符串
    }

    public function process(){

        return $this->hasMany(ActivityProcess::class,'activity_id','id');

    }


    public function order(){

        return $this->hasMany(Order::class,'activity_id','id')->select(['id','activity_id','person']);

    }


    public function favorite(){

        return $this->hasOne(UserFavorite::class,'activity_id','id')->select(['id','activity_id']);

    }

    public function order_user(){

        return $this->hasOne(OrderUser::class,'activity_id','id');

    }


}
